﻿using AutoMapper;
using B.S.BaseData.Domain;
using B.S.BaseData.ErrorCode;
using B.S.BaseData.Infratructure.Interfares;
using B.S.BaseData.Write.Api.Application.Command;
using MediatR;

namespace B.S.BaseData.Write.Api.Application.CommandHander
{
    public class AddLeaveCommandHander : IRequestHandler<AddLeaveCommand, ApiResult<int>>
    {

        private readonly IMapper mapper;
        private readonly IBaseRequsitory<Leave> baseRequsitory;
        private readonly IBaseRequsitory<WF_Setting> settingRequsitory;
        private readonly IBaseRequsitory<WF_Instafre> instaresRequsitory;
        private readonly IBaseRequsitory<WF_Recode> recodeRequsitory;

        public AddLeaveCommandHander(IMapper mapper, IBaseRequsitory<Leave> baseRequsitory, IBaseRequsitory<WF_Setting> settingRequsitory, IBaseRequsitory<WF_Instafre> instaresRequsitory, IBaseRequsitory<WF_Recode> recodeRequsitory)
        {
            this.mapper = mapper;
            this.baseRequsitory = baseRequsitory;
            this.settingRequsitory = settingRequsitory;
            this.instaresRequsitory = instaresRequsitory;
            this.recodeRequsitory = recodeRequsitory;
        }

        /// <summary>
        /// 添加请假申请处理
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ApiResult<int>> Handle(AddLeaveCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();

            DateTime now = DateTime.Now;
            #region app表
            var entity = mapper.Map<Leave>(request);
            await baseRequsitory.AddAsync(entity);
            #endregion

            Leave app = new Leave();
            #region 实例表
            WF_Instafre instafre = new WF_Instafre();
            instafre.leaveId = entity.LeaveId;
            instafre.UserId = request.OperatorId;
            instafre.UserName = request.LeaveName;

            instafre.StarterNode = (int)AppEnum.已提交;
            instafre.StarterName = AppEnum.已提交.ToString();

            var setting = settingRequsitory.GetAllAsync().OrderBy(x => x.NodeId).ToList();

            instafre.PextStarterNodeNo = setting[0].NodeId;
            instafre.PextStarterName = setting[0].NodeName;
            instafre.PextOperNo = request.OperatorId.ToString();
            instafre.PextOperName = request.LeaveName;

            instafre.NextStarterNodeNo = setting[1].NodeId;
            instafre.NextStarterName = setting[1].NodeName;
            instafre.NextOperNo = setting[1].UserId.ToString();
            instafre.NextOperName = setting[1].UserName;

            instafre.CreateTime = now;

            await instaresRequsitory.AddAsync(instafre);

            #endregion

            #region 流程记录表

            WF_Recode recode = new WF_Recode();

            recode.InstafreId = instafre.InstafreId;
            recode.LeaveId = instafre.leaveId;
            recode.StarterNodeNo = (int)RecodeEnum.已提交;
            recode.EnderNodeName = RecodeEnum.已提交.ToString();
            recode.UserId = request.OperatorId;
            recode.UserName = request.LeaveName;
            recode.YiJian = "";
            recode.CreateTime = now;

            await recodeRequsitory.AddAsync(recode);

            #endregion

            result.Code = ApiEnum.添加成功;
            result.Msg = ApiEnum.添加成功.ToString();
            result.Data = 0;
            return await Task.FromResult(result);
        }
    }
}
